package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.mapper.ContainerAdviceMapper;
import com.foreverwin.jzymes.common.service.IContainerAdviceService;
import com.foreverwin.jzymes.common.service.IItemAdviceService;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

@Service
public class ContainerAdviceServiceImpl implements IContainerAdviceService {

    @Autowired
    ContainerAdviceMapper containerAdviceMapper;
    @Autowired
    IItemAdviceService itemAdviceService;

    /**
     * 查询包装容器
     *
     * @param site
     * @param container
     * @return
     */
    public Map<String,Object> findContainer( String site, String container ){
        /**检查站点*/
        Assert.hasText( site, I18nUtil.getI18nText("common.siteNotBlank"));
        /**检查容器编号*/
        Assert.hasText( container, I18nUtil.getI18nText( "common.containerNotBlank" ) );

        return containerAdviceMapper.selectContainer( site, container );
    }

    /**
     * 检查包装容器是否存在
     *
     * @param site
     * @param container
     * @return
     */
    public Map<String,Object> existContainer( String site, String container ){
        Map<String,Object> containerMap = findContainer( site, container );
        Assert.notNull( containerMap, I18nUtil.getI18nText( "common.containerNotExist", new String[]{ site, container } ) );

        return containerMap;
    }

    /**
     * 查询包装级别
     *
     * @param site
     * @param container
     * @param itemBo
     * @return
     */
    public Map<String,Object> findItemPackingLevel( String site, String container, String itemBo ){

        /**检查容器*/
        Map<String,Object> containerMap = existContainer( site, container );
        /**检查物料*/
        Map<String,Object> itemMap = itemAdviceService.existItem( itemBo );

        Map<String,Object> packingLevelMap = containerAdviceMapper.selectContainerPackingLevel( (String)containerMap.get( "HANDLE" ), "ITEM", (String)itemMap.get( "ITEM" ), (String)itemMap.get("REVISION") );
        if( packingLevelMap == null ){
            packingLevelMap = containerAdviceMapper.selectContainerPackingLevel( (String)containerMap.get( "HANDLE" ), "ITEM", (String)itemMap.get( "ITEM" ), "*" );
        }
        if( packingLevelMap == null ){
            packingLevelMap = containerAdviceMapper.selectContainerPackingLevel( (String)containerMap.get( "HANDLE" ), "ITEM", (String)itemMap.get( "ITEM" ), "#" );
        }
        return packingLevelMap;
    }

    /**
     * 检查物料包装级别是否存在
     *
     * @param site
     * @param container
     * @param itemBo
     * @return
     */
    public Map<String,Object> existPackingLevel( String site, String container, String itemBo ){
        Map<String,Object> packingLevelMap = findItemPackingLevel( site, container, itemBo );
        Assert.notNull( packingLevelMap, I18nUtil.getI18nText( "common.containerPackingLevelNotExist", new String[]{ container, itemBo } ) );

        return packingLevelMap;
    }

    /**
     * 查询物料包装级别
     *
     * @param itemBo
     * @return
     */
    public List<Map<String, Object>> findItemPackingLevel( String site, String itemBo ){
        /**检查物料*/
        Map<String,Object> itemMap = itemAdviceService.existItem( itemBo );

        List<Map<String,Object>> packingLevelList = containerAdviceMapper.selectContainerPackingLevelByItem( site, (String)itemMap.get( "ITEM" ), (String)itemMap.get("REVISION") );
        if( packingLevelList == null || packingLevelList.size() == 0 ){
            packingLevelList = containerAdviceMapper.selectContainerPackingLevelByItem( site, (String)itemMap.get( "ITEM" ), "*" );
        }
        if( packingLevelList == null || packingLevelList.size() == 0 ){
            packingLevelList = containerAdviceMapper.selectContainerPackingLevelByItem( site, (String)itemMap.get( "ITEM" ), "#" );
        }
        return packingLevelList;
    }
}
